﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
namespace DTcms.DAL
{
	/// <summary>
	/// 数据访问类:foreman_apply
	/// </summary>
	public partial class foreman_apply
    {
        private string databaseprefix; //数据库表名前缀
        public foreman_apply(string _databaseprefix)
        {
            databaseprefix = _databaseprefix;
        }
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from " + databaseprefix + "foreman_apply");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(DTcms.Model.foreman_apply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into " + databaseprefix + "foreman_apply(");
			strSql.Append("foreman_id,apply_type,apply_name,apply_phone,apply_city,apply_summary,apply_info,apply_src,apply_status,apply_addtime,apply_handler,apply_handtime,apply_note)");
			strSql.Append(" values (");
			strSql.Append("@foreman_id,@apply_type,@apply_name,@apply_phone,@apply_city,@apply_summary,@apply_info,@apply_src,@apply_status,@apply_addtime,@apply_handler,@apply_handtime,@apply_note)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@foreman_id", SqlDbType.Int,4),
					new SqlParameter("@apply_type", SqlDbType.Int,4),
					new SqlParameter("@apply_name", SqlDbType.NVarChar,50),
					new SqlParameter("@apply_phone", SqlDbType.VarChar,20),
					new SqlParameter("@apply_city", SqlDbType.NVarChar,100),
					new SqlParameter("@apply_summary", SqlDbType.NVarChar,255),
					new SqlParameter("@apply_info", SqlDbType.Text),
					new SqlParameter("@apply_src", SqlDbType.Int,4),
					new SqlParameter("@apply_status", SqlDbType.Int,4),
					new SqlParameter("@apply_addtime", SqlDbType.DateTime),
					new SqlParameter("@apply_handler", SqlDbType.Int,4),
					new SqlParameter("@apply_handtime", SqlDbType.DateTime),
					new SqlParameter("@apply_note", SqlDbType.Text)};
			parameters[0].Value = model.foreman_id;
			parameters[1].Value = model.apply_type;
			parameters[2].Value = model.apply_name;
			parameters[3].Value = model.apply_phone;
			parameters[4].Value = model.apply_city;
			parameters[5].Value = model.apply_summary;
			parameters[6].Value = model.apply_info;
			parameters[7].Value = model.apply_src;
			parameters[8].Value = model.apply_status;
			parameters[9].Value = model.apply_addtime;
			parameters[10].Value = model.apply_handler;
			parameters[11].Value = model.apply_handtime;
			parameters[12].Value = model.apply_note;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DTcms.Model.foreman_apply model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update " + databaseprefix + "foreman_apply set ");
			strSql.Append("foreman_id=@foreman_id,");
			strSql.Append("apply_type=@apply_type,");
			strSql.Append("apply_name=@apply_name,");
			strSql.Append("apply_phone=@apply_phone,");
			strSql.Append("apply_city=@apply_city,");
			strSql.Append("apply_summary=@apply_summary,");
			strSql.Append("apply_info=@apply_info,");
			strSql.Append("apply_src=@apply_src,");
			strSql.Append("apply_status=@apply_status,");
			strSql.Append("apply_addtime=@apply_addtime,");
			strSql.Append("apply_handler=@apply_handler,");
			strSql.Append("apply_handtime=@apply_handtime,");
			strSql.Append("apply_note=@apply_note");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@foreman_id", SqlDbType.Int,4),
					new SqlParameter("@apply_type", SqlDbType.Int,4),
					new SqlParameter("@apply_name", SqlDbType.NVarChar,50),
					new SqlParameter("@apply_phone", SqlDbType.VarChar,20),
					new SqlParameter("@apply_city", SqlDbType.NVarChar,100),
					new SqlParameter("@apply_summary", SqlDbType.NVarChar,255),
					new SqlParameter("@apply_info", SqlDbType.Text),
					new SqlParameter("@apply_src", SqlDbType.Int,4),
					new SqlParameter("@apply_status", SqlDbType.Int,4),
					new SqlParameter("@apply_addtime", SqlDbType.DateTime),
					new SqlParameter("@apply_handler", SqlDbType.Int,4),
					new SqlParameter("@apply_handtime", SqlDbType.DateTime),
					new SqlParameter("@apply_note", SqlDbType.Text),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.foreman_id;
			parameters[1].Value = model.apply_type;
			parameters[2].Value = model.apply_name;
			parameters[3].Value = model.apply_phone;
			parameters[4].Value = model.apply_city;
			parameters[5].Value = model.apply_summary;
			parameters[6].Value = model.apply_info;
			parameters[7].Value = model.apply_src;
			parameters[8].Value = model.apply_status;
			parameters[9].Value = model.apply_addtime;
			parameters[10].Value = model.apply_handler;
			parameters[11].Value = model.apply_handtime;
			parameters[12].Value = model.apply_note;
			parameters[13].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from " + databaseprefix + "foreman_apply ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from " + databaseprefix + "foreman_apply ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.foreman_apply GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,foreman_id,apply_type,apply_name,apply_phone,apply_city,apply_summary,apply_info,apply_src,apply_status,apply_addtime,apply_handler,apply_handtime,apply_note from " + databaseprefix + "foreman_apply ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			DTcms.Model.foreman_apply model=new DTcms.Model.foreman_apply();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DTcms.Model.foreman_apply DataRowToModel(DataRow row)
		{
			DTcms.Model.foreman_apply model=new DTcms.Model.foreman_apply();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["foreman_id"]!=null && row["foreman_id"].ToString()!="")
				{
					model.foreman_id=int.Parse(row["foreman_id"].ToString());
				}
				if(row["apply_type"]!=null && row["apply_type"].ToString()!="")
				{
					model.apply_type=int.Parse(row["apply_type"].ToString());
				}
				if(row["apply_name"]!=null)
				{
					model.apply_name=row["apply_name"].ToString();
				}
				if(row["apply_phone"]!=null)
				{
					model.apply_phone=row["apply_phone"].ToString();
				}
				if(row["apply_city"]!=null)
				{
					model.apply_city=row["apply_city"].ToString();
				}
				if(row["apply_summary"]!=null)
				{
					model.apply_summary=row["apply_summary"].ToString();
				}
				if(row["apply_info"]!=null)
				{
					model.apply_info=row["apply_info"].ToString();
				}
				if(row["apply_src"]!=null && row["apply_src"].ToString()!="")
				{
					model.apply_src=int.Parse(row["apply_src"].ToString());
				}
				if(row["apply_status"]!=null && row["apply_status"].ToString()!="")
				{
					model.apply_status=int.Parse(row["apply_status"].ToString());
				}
				if(row["apply_addtime"]!=null && row["apply_addtime"].ToString()!="")
				{
					model.apply_addtime=DateTime.Parse(row["apply_addtime"].ToString());
				}
				if(row["apply_handler"]!=null && row["apply_handler"].ToString()!="")
				{
					model.apply_handler=int.Parse(row["apply_handler"].ToString());
				}
				if(row["apply_handtime"]!=null && row["apply_handtime"].ToString()!="")
				{
					model.apply_handtime=DateTime.Parse(row["apply_handtime"].ToString());
				}
				if(row["apply_note"]!=null)
				{
					model.apply_note=row["apply_note"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,foreman_id,apply_type,apply_name,apply_phone,apply_city,apply_summary,apply_info,apply_src,apply_status,apply_addtime,apply_handler,apply_handtime,apply_note ");
			strSql.Append(" FROM " + databaseprefix + "foreman_apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,foreman_id,apply_type,apply_name,apply_phone,apply_city,apply_summary,apply_info,apply_src,apply_status,apply_addtime,apply_handler,apply_handtime,apply_note ");
			strSql.Append(" FROM " + databaseprefix + "foreman_apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM " + databaseprefix + "foreman_apply ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from " + databaseprefix + "foreman_apply T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "" + databaseprefix + "foreman_apply";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod
        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM view_foreman_apply");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
		#endregion  ExtensionMethod
	}
}

